<?php
namespace common\models;

use common\models\TallyAttachment;
use common\services\TallyService;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "tally_record".
 *
 * @property int $id 账单记录ID
 * @property int $uid 创建者
 * @property int $tally_id 账本ID
 * @property int $category_id 类别ID
 * @property string $category_name 类别
 * @property int $category_pid 一级类别ID
 * @property string $category_pname 一级类别
 * @property int $account_id 账户ID
 * @property int $account_dst_id 目标账户ID
 * @property string $account_name 账户
 * @property string $account_dst_name 目标账户
 * @property int $project_id 项目ID
 * @property string $project_name 项目
 * @property int $store_id 商家ID
 * @property string $store_name 商家
 * @property int $member_id 成员ID
 * @property string $member_name 成员
 * @property string $tally_time 账单时间
 * @property int $type 账目类型10收入20支出30转账
 * @property string $amount 金额
 * @property string $memo 备注
 * @property string $created_time 创建时间
 * @property string $updated_time 更新时间
 * @property string $audit_uid 审核人员
 * @property string $audit_time 审核时间
 * @property string $state 记录状态
 */
class TallyRecord extends \yii\db\ActiveRecord
{

    /**
     * 账目类型
     */
    const TYPE_IN       = 10; // 收入
    const TYPE_OUT      = 20; // 支出
    const TYPE_TRANS    = 30; // 转账

    /**
     * 记录状态
     */
    const STATE_INVALID = 0; //状态等待审核
    const STATE_VALID   = 1; //状态正常
    const STATE_REFUSED = -1; //状态拒绝 审核未通过

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tally_record';
    }

    public static function getTypeSourceTxt($lv): string
    {
        if($lv['arap_id'] != 0){
            $type = $lv['type'];
            if($type == self::TYPE_IN) {
                return '应收';
            }else if($type == self::TYPE_OUT) {
                return '应付';
            }
        }else if($lv['future_id'] != 0){
            $type = $lv['type'];
            if($lv['is_refund'] != 0) {
                if($type == self::TYPE_IN) {
                    return '预付退款';
                }else if($type == self::TYPE_OUT) {
                    return '预收退款';
                }
            }
            if($type == self::TYPE_IN) {
                return '预收';
            }else if($type == self::TYPE_OUT) {
                return '预付';
            }
        }
        return '记一笔';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'tally_id', 'category_id','arap_id','category_pid', 'account_id','account_dst_id', 'project_id', 'store_id', 'member_id', 'type','stock_id'], 'integer'],
            [['amount'], 'number'],
            [['created_time', 'updated_time','tally_time'], 'safe'],
            [['category_name','category_pname', 'account_name', 'account_dst_name', 'project_name', 'store_name', 'member_name'], 'string', 'max' => 60],
            [['memo'], 'string', 'max' => 255],
            [['material_json'], 'string'],
            [['state'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'category_id' => 'Category ID',
            'arap_id' => 'Arap ID',
            'category_pid' => 'Category PID',
            'tally_id' => 'Tally ID',
            'category_name' => 'Category Name',
            'category_pname' => 'Category PName',
            'account_id' => 'Account ID',
            'account_dst_id' => 'Account Dst ID',
            'account_name' => 'Account Name',
            'account_dst_name' => 'Account Dst Name',
            'project_id' => 'Project ID',
            'project_name' => 'Project Name',
            'store_id' => 'Store ID',
            'store_name' => 'Store Name',
            'member_id' => 'Member ID',
            'member_name' => 'Member Name',
            'type' => 'Type',
            'amount' => 'Amount',
            'memo' => 'Memo',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
            'tally_time' => 'Tally Time',
            'audit_uid' => 'Audit Uid',
            'audit_time' => 'Audit Time',
            'state' => 'State'
        ];
    }

    /**
     * @param $role
     *
     * @return int|null
     * @desc 不同角色默认不同账单状态
     */
    public static function getDefaultStateByRole($role,$tally_id = 0): ?int
    {
        $state = null ;
        $tally = TallyService::getOne($tally_id);


        $financial_type = ArrayHelper::getValue($tally,'financial_type','');



        if($financial_type == 1){
            return self::STATE_VALID;
        }
        if($role == TallyPm::ROLE_ADMIN) {
            $state = self::STATE_VALID;
        }else if($role == TallyPm::ROLE_FINANCE) {
            $state = self::STATE_INVALID;
        }else if($role == TallyPm::ROLE_ACCOUNTANT) {
            $state = self::STATE_INVALID;
        }else if($role == TallyPm::ROLE_CASHIER) {
            $state = self::STATE_VALID;
        }else if($role == TallyPm::ROLE_MANAGER) {
            $state = self::STATE_VALID;
        }else if($role == TallyPm::ROLE_PROJECT_MANAGER) {
            $state = self::STATE_VALID;
        }
        return $state;
    }

    /**
     * @return \yii\db\ActiveQuery
     * @notes 获取附件列表
     */
    public function getAttachment()
    {
        return $this->hasMany(TallyAttachment::className(),['tally_record_id' => 'id']);
    }

    /**
     * @param $type
     *
     * @return string
     * @desc 获取类型文字
     */
    public static function getTypeTxt($type): string
    {
        if($type == self::TYPE_IN) {
            return '收入';
        }else if($type == self::TYPE_OUT) {
            return '支出';
        }else if($type == self::TYPE_TRANS) {
            return '转账';
        }else{
            return '未知';
        }
    }

    /**
     * @param $status
     *
     * @return string
     * @desc 获取状态文字
     */
    public static function getStatusTxt($status)
    {
        if($status == self::STATE_VALID) {
            return '已入账';
        }else if($status == self::STATE_INVALID) {
            return '未入账';
        }else if($status == self::STATE_REFUSED) {
            return '已忽略';
        }else{
            return '未知';
        }
    }
}
